import Vue from 'vue'
import VueRouter from 'vue-router'
import HomeView from '../views/HomeView.vue'
// import index from '../components/index.vue'

import {
  Toast
} from 'vant';
Vue.use(VueRouter)
// name: 'index',
// component: index,
// redirect: 'home',
// children: [{
// path: 'home',


const routes = [{
  path: '/',
  name: 'home',
  component: HomeView,
  meta: {
    keep: true
  }
}, {
  path: '/issue',
  name: 'issue',
  component: () => import('../views/issue.vue'),
  meta: {
    auth: true,
    keep: false
  }
}, {
  path: '/my',
  name: 'my',
  component: () => import('../views/my.vue'),
  meta: {
    keep: false
  }
},
// ]
// },
{
  path: '/about',
  name: 'about',

  component: () => import('../views/AboutView.vue'),
  meta: {
    keep: false
  }
},
{
  path: '/userinfo',
  name: 'userinfo',
  component: () => import('../views/userinfo.vue'),
  meta: {
    auth: true,
    keep: false
  }
},
{
  path: '/login',
  name: 'login',
  component: () => import('../views/login.vue'),
  meta: {
    keep: false
  }

},
{
  path: '/reg',
  name: 'reg',
  component: () => import('../views/reg.vue'),
  meta: {
    keep: false
  }
},
{
  path: '/search',
  name: 'search',
  component: () => import('../views/search.vue'),
  meta: {
    keep: true
  }
},
{
  path: '/detail',
  name: 'detail',
  component: () => import('../views/detail.vue'),
  meta: {
    keep: false
  }
},
{
  path: '/eatle',
  name: 'eatle',
  component: () => import('../views/eatle.vue')
},
{
  path: '/forgetpassword',
  name: 'forgetpassword',
  component: () => import('../views/forgetpassword.vue'),
  meta: {
    keep: false
  }
},
{
  path: '/collectlist',
  name: 'collectlist',
  component: () => import('../views/collectlist.vue'),
  meta: {
    keep: false
  }
},
{
  path: '/searchList',
  name: 'searchList',
  component: () => import('../views/searchList.vue'),
  meta: {
    keep: false
  }
},
]

const router = new VueRouter({
  routes
})

router.beforeEach((to, from, next) => {
  // to  去到那
  // from从那来
  // next() 如果传参数就跳到那个一面

  if (to.meta.auth) {
    //判断是不是需要验证meta.auth
    // 需要验证
    // 需要验证从本地拿到token uid
    let token = localStorage.getItem('tt-token')
    let uid = localStorage.getItem('tt-uid')
    if (uid && token) {
      // 有uid token证明是登录状态
      // console.log(to)
      // console.log(from)
      next() //是登录状态不拦截
    } else {
      Toast('请先登录')
      // next('/login')
      router.replace('/login')//不是登录状态  拦截  去登录
    }
  } else {
    next()//不需要验证meta.auth  直接放行
  }

})





export default router
